//
//  MeController.m
//  SleepingElf
//
//  Created by karong on 2023/10/16.
//

#import "MeController.h"

@interface MeController ()
@property (weak, nonatomic) IBOutlet UICollectionView *listView;
@property (weak, nonatomic) IBOutlet UIImageView *userImageView;
@property (weak, nonatomic) IBOutlet UILabel *userNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *userIDLabel;
@property (weak, nonatomic) IBOutlet UIImageView *bgImageView;
@property (nonatomic, strong) NSArray * dataArray;
@end

@implementation MeController
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        self.hidesBottomBarWhenPushed = NO;
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self layoutMainView];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addObserver];
}

- (void)addObserver {
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(layoutMainView) name:SELoginSuccessNotification object:nil];
}

- (void)layoutMainView {
    if (appUserCenter.isLogin) {
        self.userNameLabel.text = SEEmptyStr(appUserCenter.nickname);
        self.userIDLabel.text = SESF(@"ID:%ld", (long)appUserCenter.id);
    }
    else {
        self.userNameLabel.text = WSLocalized(@"点击登录");
        self.userIDLabel.text = @"";
    }
    [self.userImageView sd_setImageWithURL:[NSURL URLWithString:appUserCenter.avatar] placeholderImage:SEUserPlaceholderImage];
    [self.bgImageView sd_setImageWithURL:[NSURL URLWithString:appUserCenter.avatar] placeholderImage:SEUserPlaceholderImage];
    
}

- (IBAction)messageAction:(id)sender {
    if (appUserCenter.isLogin) {
        UIViewController * target = [[CTMediator sharedInstance]ct_showWebWithUrl:API_FEEDBACK_LIST titleStr:WSLocalized(@"讯息中心") showNav:YES closeHandle:^{}];
        [self.navigationController pushViewController:target animated:YES];
    }
    else {
        UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"LoginController" bundle:nil];
        UIViewController * target = [storyBoard instantiateViewControllerWithIdentifier:@"MiniLoginPop"];
        [self presentViewController:target animated:YES completion:nil];
    }
    
}

- (IBAction)userInfoAction:(id)sender {
    if (appUserCenter.isLogin) {
        UIViewController * target = [self.storyboard instantiateViewControllerWithIdentifier:@"UserInfoController"];
        [self.navigationController pushViewController:target animated:YES];
    }
    else {
        UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"LoginController" bundle:nil];
        UIViewController * target = [storyBoard instantiateViewControllerWithIdentifier:@"MiniLoginPop"];
        [self presentViewController:target animated:YES completion:nil];
    }
}

#pragma mark UICollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"meCell" forIndexPath:indexPath];
    NSDictionary * dic = self.dataArray[indexPath.row];
    UILabel * label = [cell viewWithTag:101];
    label.text = dic[@"title"];
    UIImageView * img = [cell viewWithTag:102];
    img.image = SEIMG(dic[@"img"]);
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGSize size = CGSizeZero;
    size.width = SEScreenWidth;
    size.height = 22 + 33;
    return size;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.row) {
        case 0:
        {
            if (appUserCenter.isLogin) {
                UIViewController * target = [self.storyboard instantiateViewControllerWithIdentifier:@"CollectController"];
                [self.navigationController pushViewController:target animated:YES];
            }
            else {
                UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"LoginController" bundle:nil];
                UIViewController * target = [storyBoard instantiateViewControllerWithIdentifier:@"MiniLoginPop"];
                [self presentViewController:target animated:YES completion:nil];
            }
        }
            break;
        case 1:
        {
            UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"HomeController" bundle:nil];
            UIViewController * target = [storyBoard instantiateViewControllerWithIdentifier:@"HistoryController"];
            [self.navigationController pushViewController:target animated:YES];
        }
            break;
        case 2:
        {
            UIViewController * target = [self.storyboard instantiateViewControllerWithIdentifier:@"SettingController"];
            [self.navigationController pushViewController:target animated:YES];
            
        }
            break;
        case 3:
        {
            UIViewController * target = [self.storyboard instantiateViewControllerWithIdentifier:@"LanguagePicker"];
            target.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
            target.modalPresentationStyle = UIModalPresentationCustom;
            [self presentViewController:target animated:YES completion:nil];
        }
            break;
        case 4:
        {
            if (appUserCenter.isLogin) {
                UIViewController * target = [self.storyboard instantiateViewControllerWithIdentifier:@"FeedbackController"];
                [self.navigationController pushViewController:target animated:YES];
            }
            else {
                UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"LoginController" bundle:nil];
                UIViewController * target = [storyBoard instantiateViewControllerWithIdentifier:@"MiniLoginPop"];
                [self presentViewController:target animated:YES completion:nil];
            }
        }
        default:
            break;
    }

}

- (NSArray *)dataArray {
    if (!_dataArray) {
        _dataArray = @[@{@"title":WSLocalized(@"我的收藏"),
                         @"img":@"icon_wdsc"},
                       @{@"title":WSLocalized(@"播放历史"),
                         @"img":@"icon_bfls"},
                       @{@"title":WSLocalized(@"设置"),
                         @"img":@"icon_sz"},
                       @{@"title":WSLocalized(@"多语言"),
                         @"img":@"icon_dyy"},
                       @{@"title":WSLocalized(@"意见反馈"),
                         @"img":@"icon_yjfk"}];
    }
    return _dataArray;
}


@end
